package com.woniuxy.cq.phrase3.dao;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.cq.phrase3.entity.Project;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniuxy.cq.phrase3.entity.ProjectIndustryUser;
import com.woniuxy.cq.phrase3.entity.UserBm;
import com.woniuxy.cq.phrase3.entity.UserProject;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;


import java.util.Date;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author Lucas
 * @since 2020-09-07
 */
public interface ProjectMapper extends BaseMapper<Project> {

        @Insert("insert into tb_sx(sx_bm_id,sx_project_id,sx_user_id) VALUES (#{sx_bm_id},#{sx_project_id},#{sx_user_id})")
        public void insertSxByUser(@Param("sx_bm_id") Integer bmId,@Param("sx_project_id") Integer projectId, @Param("sx_user_id") Integer userId);


        @Select("select user_office_id from tb_user where user_name = #{userName}")
        public Integer selectBmByuserName(String userName);

        @Select("select user_id from tb_user where user_name = #{userName}")
        public Integer selectIdByUserName(String userName);

        @Select("select b.bm_id,b.bm_name,u.user_id,u.user_name from \n" +
                "tb_user u,tb_bm b\n" +
                "where u.user_office_id = b.bm_id\n")
        public List<UserBm> selectAllByUserBm();

        /**
         * 查询需要受理的项目
         * @param checkStatusId 审核状态
         */
        @Select("select * \n" +
                "from tb_project p,tb_user u\n" +
                "where project_status = 'UN_ACCEPT' and u.user_id = p.project_user_id ")
        public IPage<UserProject> acceptedItems(Page<UserProject> page, @Param("checkStatusId")Integer checkStatusId);

        /**
         * 修改项目状态
         */
        @Update("update tb_project set project_status=#{project_status},project_update_time=#{time} where project_id=#{project_id}")
        public void updateProjectStatus(@Param("project_status") String projectStatus,@Param("time")Date currentTime,@Param("project_id")Integer projectId);

        @Update("update tb_project set project_status='OUT_ACCEPT_RANGE',project_result='不属于受理范围',project_update_time=#{time} where project_id =#{id}")
        public void  outAcceptRange(@Param("id") Integer projectId, @Param("time")Date currentTime);

        /**
         * 修改项目状态为 资料不齐全
         */
        @Update("update tb_project set project_status='INCOMPLETE_DATA',project_result='资料不齐全',project_update_time=#{time} where project_id =#{id}")
        public void  incompleteData(@Param("id") Integer projectId, @Param("time")Date currentTime);

        /**
         * 修改项目状态为 成功受理
         */
        @Update("update tb_project set project_status='SUCCESS_ACCEPT',project_result='成功受理',project_sx_total=#{sxTotal},project_update_time=#{time} where project_id =#{id}")
        public void  successAccept(@Param("id") Integer projectId, @Param("sxTotal") Integer sxTotal,@Param("time")Date currentTime);


        /**
         * 局长 查询所需要处理的事项
         * @param industryTypeId        领导所属部门Id
         * @return
         */
        @Select("select * from tb_project where industry_type_id = 1 and project_status = 'SUCCESS_CHECK'  or project_status = 'SUCCESS_DIRECTOR'")
        public List<Project> leaderListByChairman(Integer industryTypeId);

        /**
         * 局长审批通过准备继续交由省政府决定的
         * @param projectId     项目Id
         * @return
         */
        @Update("UPDATE tb_project set project_status='SUCCESS_CHAIRMAN',project_update_time=#{time} where project_id =#{id}")
        public void allowProjectByChairman(@Param("id") Integer projectId,@Param("time") Date currentTime);

        /**
         * 局长直接审批通过的决定
         * @param projectId             项目Id
         * @return
         */
        @Update("UPDATE tb_project set project_status='SUCCESS',project_update_time=#{time} where project_id =#{id}")
        public void passProjectByChairman(@Param("id") Integer projectId,@Param("time") Date currentTime);

        /**
         * 局长审批未通过
         * @param projectId
         * @return
         */
        @Update("UPDATE tb_project set project_status='FAIL_CHAIRMAN',project_update_time=#{time} where project_id =#{id}")
        public void failProjectByChairman(@Param("id") Integer projectId,@Param("time") Date currentTime);
        /**
         * 主任查询所需要处理的事项
         * @param industryTypeId
         * @return
         */
        @Select("select * from tb_project where industry_type_id = 1 and project_status = 'SUCCESS_CHECK'")
        public List<Project> leaderListByDirector(Integer industryTypeId);

        /**
         * 主任审批直接批准项目
         * @param projectId
         * @return
         */
        @Update("UPDATE tb_project set project_status='SUCCESS',project_update_time=#{time} where project_id =#{id}")
        public void passProjectByDirector(@Param("id") Integer projectId,@Param("time") Date currentTime);
        /**
         * 主任 审批通过 需要由局长再审批的决定
         * @param projectId
         * @return
         */
        @Update("UPDATE tb_project set project_status='SUCCESS_DIRECTOR',project_update_time=#{time} where project_id =#{id}")
        public void allowProjectByDirector(@Param("id") Integer projectId,@Param("time") Date currentTime);

        /**
         * 主任审批未通过
         * @param projectId
         * @return
         */
        @Update("UPDATE tb_project set project_status='FAIL_DIRECTOR',project_update_time=#{time} where project_id =#{id}")
        public void failProjectByDirector(@Param("id") Integer projectId,@Param("time") Date currentTime);

        /**
         * 省政府审批未通过
         * @param projectId
         * @return
         */
        @Update("UPDATE tb_project set project_status='FAIL_PROVINCIAL',project_update_time=#{time} where project_id =#{id}")
        public void failProjectByProvincial(@Param("id") Integer projectId,@Param("time") Date currentTime);

        /**
         * 省政府通过
         * @param projectId
         * @return
         */
        @Update("UPDATE tb_project set project_status='SUCCESS' where project_id =#{id},project_update_time=#{time}")
        public void      passProjectByProvincial(@Param("id") Integer projectId,@Param("time") Date currentTime);
        /**
         * 领导分页查询
         * @param page projectStatus projectName
         * @return
         */
        IPage<Project> chairmanPageList(Page<Project> page, String projectStatus, String projectName);



        /**
         * @description:  分页查询项目进度
         * @param: page
         * @param: userId
         * @param: projectPhase
         * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.woniuxy.cq.phrase3.entity.Project>
         * @author Daniel
         * @date: 2020/9/9 12:02
         */
        IPage<Project> pageQueryProject(Page<Project> page, Integer userId, String projectPhase);

        @Select("select * from tb_project p left join tb_industry i on p.industry_type_id = i.industry_id LEFT JOIN tb_user u\n" +
                "on p.project_user_id = u.user_id LEFT JOIN tb_country c on p.project_country_id = c.country_id where p.project_id =#{projectId} ")
        ProjectIndustryUser getProjectInfo(Integer projectId);
}
